package com.hbx.residential_service.controller;

import com.github.pagehelper.PageInfo;
import com.hbx.residential_service.domain.Pay;
import com.hbx.residential_service.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private PayService payService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(Model model, HttpSession session, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Pay> pays = payService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(pays);//重要 是一个分页bean
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("pay-list");
        //session.getAttribute("adminname");
        model.addAttribute("adminname", session.getAttribute("adminname"));
        return mv;
    }

    @RequestMapping("/add.do")
    public String addPay(Pay pay) throws Exception {
        payService.addPay(pay);
        return "redirect:findAll.do";
    }

    @RequestMapping("delete.do")
    public String deletePay(String payIds) throws Exception {
        String[] strs = payIds.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                payService.deletePay(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return "redirect:findAll.do";
    }

    @RequestMapping("/findupdbyid.do")
    public ModelAndView findUpdPayById(int payId) {
        Pay pay = payService.findUpdPayById(payId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pay", pay);
        mv.setViewName("pay-update");
        return mv;
    }

    @RequestMapping("/updbyid.do")
    public String UpdateById(Pay pay) {
        payService.UpdateById(pay);
        return "redirect:findAll.do";
    }

    @RequestMapping("/fuzzysearchbyusername.do")
    public ModelAndView fsByUsername(Model model,HttpSession session,String content,@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
        content = new String(content.getBytes("iso-8859-1"),"utf-8");
        List<Pay> fspy = payService.fsByUsername(content,page,size);
        PageInfo pageInfo = new PageInfo(fspy);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("content",content);
        mv.setViewName("pay-search");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

    @RequestMapping("/findByUnpaid.do")
    public ModelAndView findByUnpaid(Model model, HttpSession session, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Pay> pays = payService.findByUnpaid(page, size);
        PageInfo pageInfo = new PageInfo(pays);//重要 是一个分页bean
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("pay-unpaid");
        //session.getAttribute("adminname");
        model.addAttribute("adminname", session.getAttribute("adminname"));
        return mv;
    }
}
